// import { User } from "src/user/entities/user.entity";
import { User } from '../../user/entities/user.entity'; //改为相对路径
import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Comment } from "../../comment/entities/comment.entity";
import { Global } from '@nestjs/common';
import { UnsignedInt, utcToBeijing } from '../../utils';
import { Like } from '../../like/entities/like.entity';
import { Star } from '../../star/entities/star.entity';
@Entity({ name: 'blog' })
@Global()
export class Blog {
  @PrimaryGeneratedColumn()
  id: number;

  @CreateDateColumn({
    type: 'timestamp',
    transformer: {
      to(value: Date | null): number | null {
        if (value instanceof Date) {
          return value.getTime();
        }
        return null;
      },
      from(value: number) {
        // 将UTC时间转换为北京时间
        return utcToBeijing(value);
      },
    },
  })
  createDate: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    transformer: {
      to(value: Date | null): number | null {
        if (value instanceof Date) {
          return value.getTime();
        }
        return null;
      },
      from(value: number) {
        // 将UTC时间转换为北京时间
        return utcToBeijing(value);
      },
    },
  })
  updateDate: Date;

  @Column({
    comment: '文章标题',
    length: 50,
  })
  article_title: string;

  @Column({
    comment: '作者ID',
    nullable: false,
  })
  userId: number;

  @Column({
    comment: '分类',
  })
  category_id: number;

  @Column({
    comment: '文章描述',
    length: 50,
  })
  article_description: string;

  @Column({
    comment: '文章内容',
    length: 5000,
  })
  article_content: string;

  @Column({
    comment: '文章背景图片',
  })
  article_cover: string;

  @Column({
    comment: '是否置顶',
    default: 0,
  })
  is_top: number;

  @Column({
    comment: '排序方式',
    default: 1,
  })
  order: number;

  @Column({
    comment: '状态, 0逻辑删除, 1公开, 2私密, 3草稿',
    default: 1,
  })
  status: number;

  @Column({
    comment: '类型',
  })
  type: number;

  @Column({
    comment: '来源',
    nullable: true,
  })
  origin_url: string;

  @Column({
    type: 'int',
    unsigned: true,
    comment: '浏览次数',
    default: 0,
  })
  view_times: UnsignedInt;

  @Column({
    type: 'int',
    unsigned: true,
    comment: '点赞次数',
    default: 0,
  })
  thumbs_up_times: UnsignedInt;

  @Column({
    type: 'int',
    unsigned: true,
    comment: '收藏次数',
    default: 0,
  })
  star_times: UnsignedInt;

  @Column({
    type: 'int',
    unsigned: true,
    comment: '评论次数',
    default: 0,
  })
  comment_times: UnsignedInt;

  @Column({
    type: 'int',
    unsigned: true,
    comment: '停留时间',
    default: 0,
  })
  reading_duration: number;

  @Column({
    comment: '标签出现次数',
    default: 0,
  })
  tagIdList: string;

  @Column({
    comment: '标签名',
    nullable: true,
  })
  tagNameList: string;

  @Column({
    comment: '作者名称',
  })
  authorName: string;

  @Column({
    comment: '目录名称',
  })
  categoryName: string;

  @ManyToOne(() => User, (userInfo) => userInfo.blogs)
  user: User[];

  @OneToMany(()=>Star, (star) => star.blog)
  stars: Star[]

  @OneToMany(() => Like, (like) => like.blog)
  likes: Like[];

  @OneToMany(() => Comment, (comment) => comment.blog)
  comments: Comment[];
}
